package com.example.studentmanagement;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentmanagement.dao.StudentDao;
import com.example.studentmanagement.entity.Student;
import com.example.studentmanagement.util.DBUtil;
import com.example.studentmanagement.view.Bottom_Bar;
import com.example.studentmanagement.view.Top_Bar;

import java.util.List;

public class StudentListActivity extends AppCompatActivity {

    private StudentDao studentdao ;

    private Top_Bar top_bar ;
    private Bottom_Bar bottom_bar ;
    private ListView listView ;
    private ArrayAdapter<Student> adapter ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_studentlist);



        DBUtil dbUtil = new DBUtil(StudentListActivity.this);
        SQLiteDatabase db = dbUtil.getWritableDatabase();
        DBUtil.db=db;

        listView = findViewById(R.id.studentlistView);
        top_bar = findViewById(R.id.studentlist_top);
        bottom_bar = findViewById(R.id.studentlist_bottom);
        studentdao = new StudentDao();

        SharedPreferences sharedPreferences = getSharedPreferences("loginId" , Context.MODE_PRIVATE);
        String s_id = sharedPreferences.getString("s_id", "");




        List<Student> students = StudentDao.getAllStudent() ;


        adapter = new ArrayAdapter<com.example.studentmanagement.entity.Student>(this ,
                R.layout.studentlist , R.id.student_name,students){
            @Override
            public View getView(int position , View converView , ViewGroup parent){
                View view = super.getView(position , converView , parent);
                TextView textName = view.findViewById(R.id.student_id);
                TextView textId = view.findViewById(R.id.student_name);
                Student student =  getItem(position);

                if (student != null) {

                        textName.setText(String.valueOf(student.getName()));
                        textId.setText(String.valueOf(student.getUserid()));



                }

                return view ;
            }

        };


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView getId = view.findViewById(R.id.student_id);
                String studentId = getId.getText().toString() ;
                new AlertDialog.Builder(StudentListActivity.this)
                        .setMessage("请选择您的操作")
                        .setPositiveButton("查看信息", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent =new Intent(StudentListActivity.this,StudentInformationActivity.class);
                                intent.putExtra("s_id", s_id);
                                startActivity(intent);
                            }
                        })
                        .setNeutralButton("删除同学", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                new AlertDialog.Builder(StudentListActivity.this)
                                        .setMessage("确定删除该同学吗？")
                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                studentdao.deleteStudent(studentId);
                                                Toast.makeText(StudentListActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                                // 删除操作成功后，刷新学生列表
                                                List<Student> updatedStudents = StudentDao.getAllStudent();
                                                adapter.clear();
                                                adapter.addAll(updatedStudents);
                                                adapter.notifyDataSetChanged();
                                            }
                                        })
                                        .setNegativeButton("取消", null) // 点击取消，对话框消失，不执行任何操作
                                        .show();
                            }
                        })
                        .show();

            }
        });

        listView.setAdapter(adapter);
        top_bar.setLeftBtnIsVisible(true);//不显示
        top_bar.setRightBtnNoVisible(true);
        top_bar.setTitleText("同学");
        bottom_bar.setStudentBtnBackground(R.color.q2);
        bottom_bar.setCourseBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StudentListActivity.this, CurriculumActivity.class);
                startActivity(intent);
                finish();
            }
        });
        bottom_bar.setPersonBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StudentListActivity.this, PersonalActivity.class);
                startActivity(intent);
                finish();
            }
        });



    }
}